package com.blrs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.DemandReception;
import com.blrs.service.demand.IDemandReleaseService;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.DemandRelease;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 需求发布Controller
 * 
 * @author yang
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/demand/release")
public class DemandReleaseController extends BaseController
{
    @Autowired
    private IDemandReleaseService demandReleaseService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询需求发布列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DemandRelease demandRelease)
    {
        startPage();
        demandRelease.setBelongTo(SecurityUtils.getOrgId());
        List<DemandRelease> list = demandReleaseService.selectDemandReleaseList(demandRelease);
        return getDataTable(list);
    }

    /**
     * 查询当前用户最新的15条需求接收信息
     */
    @GetMapping("/demandIndex")
    public TableDataInfo demandIndex(DemandRelease demandRelease){
        startPage();
        List<DemandRelease> list = demandReleaseService.selectDemandReleaseListByReception(demandRelease);
        return getDataTable(list);
    }

    /**
     * 查看待审核的列表（只有平台端的人员可以审核）
     */
    @GetMapping("/Audit")
    public TableDataInfo auditList(DemandRelease demandRelease){
        startPage();
        List<DemandRelease> listdemandReleaseService=demandReleaseService.selectDemandReleaseListByAudit(demandRelease);
        return getDataTable(listdemandReleaseService);
    }

    /**
     * 查看当前用户审核通过的接收数据
     */
    @GetMapping("/receive")
    public TableDataInfo receiveList(DemandRelease demandRelease){
        startPage();
        demandRelease.setBelongTo(SecurityUtils.getOrgId());
        List<DemandRelease> listdemandReleaseService=demandReleaseService.selectDemandReleaseListByReception(demandRelease);
        return getDataTable(listdemandReleaseService);
    }


    /**
     * 导出需求发布列表
     */
    @Log(title = "需求发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DemandRelease demandRelease)
    {
        List<DemandRelease> list = demandReleaseService.selectDemandReleaseList(demandRelease);
        ExcelUtil<DemandRelease> util = new ExcelUtil<DemandRelease>(DemandRelease.class);
        util.exportExcel(response, list, "需求发布数据");
    }

    /**
     * 获取需求发布详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return success(demandReleaseService.selectDemandReleaseByProjectId(projectId));
    }

    /**
     * 新增需求发布
     */
    @Log(title = "需求发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemandRelease demandRelease) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(demandReleaseService.insertDemandRelease(demandRelease));
    }

    /**
     * 修改需求发布
     */
    @Log(title = "需求发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemandRelease demandRelease)
    {
        return toAjax(demandReleaseService.updateDemandRelease(demandRelease));
    }

    /**
     * 删除需求发布
     */
    @Log(title = "需求发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return toAjax(demandReleaseService.deleteDemandReleaseByProjectIds(projectIds));
    }
    /**
     * 一键发送终止审核
     */
    @PostMapping("/termination")
    public AjaxResult termination(@RequestBody DemandRelease demandRelease){
        return toAjax(demandReleaseService.terminationPolicy(demandRelease));
    }
    /**
     * 审核通过
     */
    @PostMapping("/examine")
    public AjaxResult examineById(@RequestBody DemandRelease demandRelease){
        return toAjax(demandReleaseService.updateStatus(demandRelease));
    }

    /**
     * 参与竞标
     */
    @GetMapping("/bid/{projectId}")
    public AjaxResult bid(@PathVariable("projectId") String projectId){
        return toAjax(demandReleaseService.bid(projectId));
    }

    /**
     * 查询所有参与竞标的企业
     */
    @GetMapping("/bidder")
    public TableDataInfo bidder(DemandRelease demandRelease){
        startPage();
        List<DemandReception> list = demandReleaseService.selectBidder(demandRelease.getProjectId());
        return getDataTable(list);
    }

    /**
     * 关闭发布的需求信息
     */
    @GetMapping("/close/{projectId}")
    public AjaxResult close(@PathVariable("projectId") String projectId){
        return toAjax(demandReleaseService.close(projectId));
    }




}
